/*
 * Main.fx
 *
 * Created on 2010-01-17, 23:08:03
 */

package galleryviewer;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.scene.text.*;
import javafx.scene.image.*;
import javafx.scene.Group;
import javafx.scene.input.*;


import javafx.stage.Screen;

import javafx.scene.transform.Transform;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;



/**
 * @author m.deluga, mr.simet, jaccus
 */

package var globalCounter = Timeline {
		repeatCount: 1
		autoReverse: false
		keyFrames : [
			KeyFrame {
				time : 60s
				values : []
                                action: function() { Effects.fadeOut(Main.currentlyOpenedWindow); }
			}
		]
	};

package var popupCounter = Timeline {
		repeatCount: 1
		autoReverse: false
		keyFrames : [
			KeyFrame {
				time : 20s
				values : []
                                action: function() { PopUp.closePopups(null); }
			}
		]
	};


package var currentlyOpenedWindow : Window = null;
package var isFullScreen = false;
package var galleryWidth :Double;
package var galleryHeight :Double;

def keyHandler = function(e:KeyEvent):Void {
    println("{e.text} pressed");
        
    if(e.code==KeyCode.VK_1 or e.code==KeyCode.VK_0) {
        GalleryFloor.setDisplayedFloor(Integer.parseInt(e.text));
    } else if(e.code==KeyCode.VK_ESCAPE) {
		currentlyOpenedWindow.close();  
    } else if(e.code==KeyCode.VK_PAGE_DOWN) {
        GalleryFloor.displayedFloorDecrement
    } else if(e.code==KeyCode.VK_PAGE_UP) {
        GalleryFloor.displayedFloorIncrement
    } else if(e.code==KeyCode.VK_F) {
        isFullScreen = not isFullScreen;
    } else if(e.code==KeyCode.VK_X) {
        stage.close();
    }
}

function scaleGallery(gWidth, gHeight):Number {
            //println("global width={Constants.FRAME_WIDTH}");
            //println("global height={Constants.FRAME_HEIGHT}");
            //println(Main.stage.width);
            //println(Main.stage.height);

            //println(gHeight);
            //println(gWidth);
            //println("-- SCALE --");
            
			var sideBorderWidth = 25;
			var bothSideBorderWidth = 2*sideBorderWidth;
			
			var gallerySpaceX = Constants.FRAME_WIDTH-bothSideBorderWidth;
            var gallerySpaceY = Constants.FRAME_HEIGHT - 100;
			
			var finalScale;
			
            if( (gallerySpaceX / gWidth)*gHeight > gallerySpaceY ) {
                finalScale = (gallerySpaceY / gHeight)  
			}
            else { 
            	finalScale = ((Constants.FRAME_WIDTH-bothSideBorderWidth) / gWidth) 
            }
			
			println("gallery finalScale={finalScale}");
			
			return finalScale;

        }

var arrowSize = 60;

var arrowUpImage = ImageView {
					image: Image {width : arrowSize height : arrowSize url: "{__DIR__}img/arrow_up.png" backgroundLoading: true}
					x: 0 y: 0
					onMouseClicked : GalleryFloor.displayedFloorIncrement
					opacity : bind if (GalleryFloor.currentFloor == Constants.LAST_FLOOR) 0.5 else 1
				}

var arrowDownImage = ImageView {
					image: Image {width : arrowSize height : arrowSize url: "{__DIR__}img/arrow_down.png" backgroundLoading: true}
					x: 0 y: 0
					onMouseClicked : GalleryFloor.displayedFloorDecrement
					opacity : bind if (GalleryFloor.currentFloor == Constants.FIRST_FLOOR) 0.5 else 1
				}

var floorSwitcher = HBox {
	nodeVPos : VPos.CENTER
	vpos : VPos.CENTER
    spacing : 15
    content : [
    	Text{ x: 20 y: 60 content: bind "POZIOM {GalleryFloor.currentFloor}" wrappingWidth: 100;
    		font: Font { size: 20 }
                fill: Color.web("#ffffff")
    	}, arrowUpImage, arrowDownImage 
    ]

    translateX: Constants.FRAME_WIDTH - 280
    translateY: bind stage.height-60
}

var background : Group = Group {

            content: [ /*Rectangle{
                            x:0 y:0
                            width: bind stage.width height: 75
                            fill: Color.web("#4e4e4e")
                            focusTraversable: true
                            onKeyPressed : keyHandler
                            }*/

                       Rectangle{
                            x:0 y:0
                            width: bind stage.width height: bind stage.height-100
                            fill: Color.web("#f7f7f7")
                            focusTraversable: true
                            onKeyPressed : keyHandler
                            }

                       Rectangle{
                            x:0 y: bind stage.height-100
                            width: bind stage.width height: 100
                            fill: Color.web("#4e4e4e")
                            focusTraversable: true
                            onKeyPressed : keyHandler
                            }
        ];

}
 	         

public var localsMap = Group {	
    var size = Group{
            content: bind GalleryFloor.floors[GalleryFloor.currentFloor].galleryContent

            var scaleNumber = scaleGallery(galleryWidth,galleryHeight);

            transforms: Transform.scale(scaleNumber,scaleNumber,0,0);

            };
    content: [  size ]

    translateX: 25
    translateY: 25

};

var spaceBetweenMapAndFloorSwitcher = 15;
var controls = Group {
	content: [ Controllers{} ]
        translateY: bind stage.height-85
        translateX: 0
}


package var stage : Stage = Stage {
    fullScreen: bind isFullScreen
    width: Screen.primary.visualBounds.width
    height: Screen.primary.visualBounds.height
    title: "Gallery"
    //width: Constants.FRAME_WIDTH
    //height: Constants.FRAME_HEIGHT
    
    scene: Scene {
        content: [ background, floorSwitcher, controls, localsMap
        ]
    }
}

override public function run() {
  stage;
}
